##################################################################
# File : /lua/platoon.lua
# Description : Platoons
# Author : GPG
##################################################################

Platoon = Class(moho.platoon_methods) {
    OnCreate = function(self, plan)
        self.Trash = TrashBag()
        
        self.LockedSquads = {}

        self.Callbacks = {
            OnDestroyed = Callback(),
        }
    end,

    OnDestroy = function(self)
        self.Callbacks.OnDestroyed:Call(self)
        self.Trash:Destroy()
    end,

    ForkThread = function(self, fn, ...)
        if fn then
            local thread = ForkThread(fn, self, unpack(arg))
            self.Trash:Add(thread)
            return thread
        else
            return nil
        end
    end,
    
    SquadNames = {
        'Attack',
        'Archers',
        'Artillery',
        'AirAttack',
        'Healers',
        'Giants',
        'Misc',
    },
    
    UnlockAllSquads = function(self)
        for k,v in self.LockedSquads do
            self.LockedSquads[k] = false
        end
    end,
    
    LockSquads = function(self, squads)
        for k,v in squads do
            self.LockedSquads[v] = true
        end
    end,
    
    UnlockSquads = function(self, squads)
        for k,v in squads do
            self.LockedSquads[v] = false
        end
    end,
    
    GetUnitCountSquads = function(self, squads)
        local count = 0
        
        for k,v in squads do
            local units = self:GetSquadUnits(v)
            
            if units then
                count = count + table.getn(units)
            end
        end
        
        return count
    end,
    
    CheckSquadExcludes = function(self, squadName, excludes)
        # Check if this squad is locked out by the platoon already
        if self.LockedSquads[squadName] then
            return false
        end
        
        if not excludes then
            return true
        end
        
        if type(excludes) == 'string' then
            if excludes == squadName then
                return false
            end
            return true
        end
        
        if type(excludes) == 'table' then
            for k,v in excludes do
                if v == squadName then
                    return false
                end
            end
            return true
        end
        
        return true
    end,
    
    MoveToLocationAllSquads = function(self, position, excludes)
        for k,v in self.SquadNames do
            if not self:CheckSquadExcludes(v, excludes) then
                continue
            end
            self:MoveToLocation( position, v )
        end
    end,
    
    AggressiveMoveToLocationAllSquads = function(self, position, excludes)
        for k,v in self.SquadNames do
            if not self:CheckSquadExcludes(v, excludes) then
                continue
            end
            self:AggressiveMoveToLocation( position, v )
        end
    end,
    
    AttackSpecificTargetAllSquads = function(self, target, excludes)
        for k,v in self.SquadNames do
            if not self:CheckSquadExcludes(v, excludes) then
                continue
            end
            self:AttackTarget( target, v )
            self:AggressiveMoveToLocation( target.Position, v )
        end
    end,
    
    StopAllSquads = function(self, excludes)
        for k,v in self.SquadNames do
            if not self:CheckSquadExcludes(v, excludes) then
                continue
            end
            self:Stop(v)
        end
    end,
    
    GuardTargetSpecificSquads = function(self, target, squads)
        for k,v in squads do
            self:GuardTarget(target, v)
        end
    end,
    
    AttackTargetSpecificSquads = function(self, target, squads)
        for k,v in squads do
            self:AttackTarget(target, v)
        end
    end,
    
    StopSpecificSquads = function(self, squads)
        for k,v in squads do
            self:Stop(v)
        end
    end,

    GetNumPlatoonUnits = function(self, excludes)
        local number = 0
        for k,v in self.SquadNames do
            if not self:CheckSquadExcludes(v, excludes) then
                continue
            end
            local squadUnits = self:GetSquadUnits( v )
            if not squadUnits then
                continue
            end
            
            number = number + table.getn( squadUnits )
        end
        return number
    end,
}